package com.example.latch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/22 下午1:55
 */
public class MySqlLatchSQLConnectionActiveThread implements Runnable {

    Logger logger = LoggerFactory.getLogger(getClass());

    private String sql = "select 1";

    private Connection connection;

    public MySqlLatchSQLConnectionActiveThread(Connection connection) {
        this.connection = connection;
    }

    public MySqlLatchSQLConnectionActiveThread(Connection connection, String sql) {
        this.connection = connection;
        this.sql = sql;
    }

    @Override
    public void run() {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.execute();
        } catch (Exception e) {
            logger.warn("心跳线程查询出错: " + e.getMessage());
        } finally {
            if (null != statement) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    logger.warn("心跳线程statement异常: " + e.getMessage());
                }
            }
        }
    }
}
